package pers.flowerinsnow.minecraft.forge.fastdungeonjoiner.gui.tab

import net.minecraft.client.Minecraft
import pers.flowerinsnow.minecraft.forge.fastdungeonjoiner.dungeon.floor.DungeonFloor
import pers.flowerinsnow.minecraft.forge.fastdungeonjoiner.manager.ConfigManager

import java.awt.event.{ActionEvent, ActionListener}
import java.awt.{BorderLayout, Dimension, FlowLayout}
import java.util
import javax.swing.event.{ListSelectionEvent, ListSelectionListener}
import javax.swing.{JButton, JList, JPanel, JScrollPane}

class DungeonTypeTab(val data : java.util.List[DungeonFloor]) extends JPanel{
  val floorList = new JList[DungeonFloor]()
  val join = new JButton(ConfigManager.Messages.menugui_join)
  val setDefault = new JButton(ConfigManager.Messages.menugui_setdefault)
  val cancelSelect = new JButton(ConfigManager.Messages.menugui_cancelselect)
  initPanel()
  initComps()
  addListener()

  private def initPanel(): Unit = {
    setLayout(new BorderLayout())
  }

  private def initComps(): Unit = {
    floorList.setListData(new util.Vector[DungeonFloor](data))
    add(new JScrollPane(floorList))

    val east = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
    east.setPreferredSize(new Dimension(100, 500))
    add(east, BorderLayout.EAST)

    join.setPreferredSize(new Dimension(100, 30))
    east.add(join)
    setDefault.setPreferredSize(new Dimension(100, 30))
    east.add(setDefault)
    cancelSelect.setPreferredSize(new Dimension(100, 30))
    east.add(cancelSelect)

    setButtonEnable()
  }
  private def addListener(): Unit = {
    floorList.addListSelectionListener(new ListSelectionListener {
      override def valueChanged(e: ListSelectionEvent): Unit = {
        setButtonEnable()
        onSelect(floorList.getSelectedValue)
      }
    })
    join.addActionListener(new ActionListener {
      override def actionPerformed(e: ActionEvent): Unit = Minecraft.getMinecraft.thePlayer.sendChatMessage("/" + floorList.getSelectedValue.cmd)
    })
    setDefault.addActionListener(new ActionListener {
      override def actionPerformed(e: ActionEvent): Unit = {
        ConfigManager.Config.fastdungeon = floorList.getSelectedValue.cmd
        ConfigManager.save()
      }
    })
    cancelSelect.addActionListener(new ActionListener {
      override def actionPerformed(e: ActionEvent): Unit = floorList.setSelectedIndex(-1)
    })
  }
  protected def onSelect(selected : DungeonFloor): Unit = {
  }
  private def setButtonEnable(): Unit = {
    val b = floorList.getSelectedIndex != -1;
    join.setEnabled(b)
    setDefault.setEnabled(b)
    cancelSelect.setEnabled(b)
  }
}
